/*
** File name:		window_factroy_reset.c
** Brief:
** COPYRIGHT(C) gkt 2019
** -------------------------------------------------------
** Module Information:
**
** Revision History --------------------------------------
** -------------------------------------------------------
**	Date 		Version		Action				Author
** -------------------------------------------------------
**	2019-11-7 	1.0			Created				liwenjie
**
*/

/* Includes -----------------------------------------------------*/
#include "gkt_types.h"
#include "gkt_debug.h"
#include "gkt_date_time.h"
#include "gkt_user.h"
#include "gkt_system.h"
#include "gkt_record_unlock.h"
#include "window_common.h"
#include "gkt_backboard.h"
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
#include "gkt_face_id.h"
#include "gkt_os.h"
#endif
/* Global Variables ---------------------------------------------*/
/* Macro --------------------------------------------------------*/
enum {
	WINDOW_FACTORY_RESET_TIMER_TODO = 0,
	WINDOW_FACTORY_RESET_TIMER_MAX
};

enum {
	WINDOW_FACTORY_RESET_ST_WAIT_CONFIRM = 0,
	WINDOW_FACTORY_RESET_ST_TODO,
	WINDOW_FACTORY_RESET_ST_DOING,
	WINDOW_FACTORY_RESET_ST_FINISH,
	WINDOW_FACTORY_RESET_ST_MAX
};

typedef struct _window_factory_reset_data {
	uint32_t	state;
	window_dialog_data_s  dialog;
} window_factory_reset_data_s;
static window_factory_reset_data_s s_window_factory_reset_data;

static void window_factory_reset_finish(void)
{
	gkt_window_voice_set_mute(0);

	g_window_ram_data.factory_reset_finish = 1;
	s_window_factory_reset_data.state = WINDOW_FACTORY_RESET_ST_FINISH;

	s_window_factory_reset_data.dialog.style = WINDOW_DIALOG_DECISION_OK;
	s_window_factory_reset_data.dialog.voice_id = WINDOW_VOICE_COMBINED_ID(operation_success);
	s_window_factory_reset_data.dialog.timeout = 0;
	gkt_window_show(WINDOW_ID_dialog, (uint32_t)&s_window_factory_reset_data.dialog);
}

static void window_factory_reset_do(void)
{
	s_window_factory_reset_data.state = WINDOW_FACTORY_RESET_ST_DOING;

#if defined(GKT_CONFIG_DOOR_TAMPER_ENABLE) && GKT_CONFIG_DOOR_TAMPER_ENABLE
	window_door_tamper_stop();
#endif

#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
#if defined(GKT_CONFIG_FACE_ID_POWER_DOMN) && GKT_CONFIG_FACE_ID_POWER_DOMN
	if(g_window_ram_data.face_id_power_state == WINDOW_DATA_FACE_ID_STATE_INIT){
		gkt_face_id_restart();
		g_window_ram_data.face_id_power_state = WINDOW_DATA_FACE_ID_STATE_SET;
		gkt_face_id_wait_ok();
	}
#endif
	gkt_face_id_send_command(MID_DELALL,NULL,0);
	g_window_ram_data.face_id_init_state = WINDOW_DATA_FACE_ID_STATE_INIT;
	gkt_thread_sleep(300);
#endif

	gkt_record_unlock_reset();
	gkt_user_reset();
	window_data_reset();

	gkt_window_system_standby_enable();
	gkt_window_timeout_enable("factory_reset_do - end");
	gkt_window_switch_control(1);
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
#if defined(GKT_CONFIG_PROXIMITY_SENSOR_ENABLE) && GKT_CONFIG_PROXIMITY_SENSOR_ENABLE
	gkt_pr_sensor_handle();
#endif
#endif
	window_factory_reset_finish();
}

static void window_factory_reset_pre_do(void)
{
	s_window_factory_reset_data.state = WINDOW_FACTORY_RESET_ST_TODO;

	window_common_mask_all();
	gkt_window_switch_control(0);
	gkt_window_system_standby_disable();
	gkt_window_timeout_disable("factory_reset_do - start");

	gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(being_factory_reset),
			GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, 
			NULL);

#if defined(GKT_CONFIG_BACKBOARD_ENABLE) && GKT_CONFIG_BACKBOARD_ENABLE
	int retval = gkt_backboard_factory_reset((GKT_BB_MODULE_NETWORK << 1U) |
			(GKT_BB_MODULE_CAT_EYE << 1U));
	if ((retval != GKT_SUCCESS) && (retval != GKT_EINPROCESS))
		gkt_error("factory_reset_do - backboard_factory_reset failed, retval=%d\n", retval);
#endif

	gkt_window_timer_set(WINDOW_FACTORY_RESET_TIMER_TODO, 
				5000, 0, 0);
}

/* Extern Functions ---------------------------------------------*/
/* Static Functions ---------------------------------------------*/
static int window_factory_reset_event_handler(
					uint32_t major_type, uint32_t minor_type, 
					uint32_t id, uint32_t data)
{
	uint32_t direct_reset;

	switch (major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type) {
				gkt_window_put_private_data(data);
				//window_common_control_key_real_trim("factory_reset - INIT", 0);
			}
			else if (GKT_WINDOW_ACTIVE == minor_type) {
				if (data) {	/* if 1, first active */
					direct_reset = gkt_window_get_private_data();
					gkt_info("window_factory_reset: <1st> direct_reset(%u)\n", direct_reset);
					if (direct_reset)
						window_factory_reset_pre_do();
					else {
						s_window_factory_reset_data.state = WINDOW_FACTORY_RESET_ST_WAIT_CONFIRM;
						window_common_mask_except_cancel_ok();
						gkt_window_set_voice(WINDOW_VOICE_COMBINED_ID(factory_reset_confirm),
								GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR);
					}
				}
				else
					gkt_window_show(GKT_WINDOW_ID_START, 0);
			}
			else if (GKT_WINDOW_UNINIT == minor_type) {
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE \
	&& defined(GKT_CONFIG_FACE_ID_POWER_DOMN) && GKT_CONFIG_FACE_ID_POWER_DOMN
				if(g_window_ram_data.face_id_power_state == WINDOW_DATA_FACE_ID_STATE_SET) {
					gkt_face_id_stop();
					g_window_ram_data.face_id_power_state = WINDOW_DATA_FACE_ID_STATE_INIT;
				}
#endif
				//window_common_control_key_real_trim("factory_reset - UNINIT", 1);
			}
			break;
		case GKT_EVENT_TIMER:
			if (WINDOW_FACTORY_RESET_ST_TODO == s_window_factory_reset_data.state) {
				gkt_window_voice_stop(0);
				window_factory_reset_do();
			}
			break;
		case GKT_EVENT_KEY:
			if (GKT_KEY_OK == minor_type) {
				gkt_window_voice_stop(0);
				window_factory_reset_pre_do();
				return GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
		case GKT_EVENT_VOICE:
			if (WINDOW_FACTORY_RESET_ST_TODO == s_window_factory_reset_data.state) {
				gkt_window_timer_clear(WINDOW_FACTORY_RESET_TIMER_TODO);
				window_factory_reset_do();
			}
			break;
#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC
		case GKT_EVENT_SYSTEM:
			return GKT_EVENT_HANDLE_COMPLETE;
#endif
		default:
			break;
	}

	return GKT_EVENT_HANDLE_CONTINUE;
}

GKT_WINDOW_DESC_DEFINE_NO_WIDGETS(factory_reset);

